package com.digitalxpert.jargle.TestGui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolTip;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.digitalxpert.completer.CompleterTextField;
import com.digitalxpert.jargle.itext.rtf.CardReportBuilder;
import com.digitalxpert.jargle.sgtable.FlexibleTable;
import com.digitalxpert.jargle.sgtable.TableHelper;

public class TabbedPane extends JTabbedPane implements ChangeListener,
		CaretListener, ActionListener {
	private Action closeA;
	JButton notModalButton = new JButton("Not modal");
	JButton modalButton = new JButton("Modal");
	JDialog owner;
	JButton copyButton = new JButton("Copy");
	FlexibleTable table;

	public TabbedPane(JDialog owner) {
		this.owner = owner;
		addChangeListener(this);
		AddressPanel address = new AddressPanel();
		JPanel myLayer = new JPanel(new BorderLayout());
		JPanel butPanel = new JPanel(new GridLayout(2, 0));
		notModalButton.addActionListener(this);
		modalButton.addActionListener(this);
		butPanel.add(notModalButton);
		butPanel.add(modalButton);
		myLayer.add(butPanel, BorderLayout.NORTH);
		JTextField field = new JTextField();
		myLayer.add(field, BorderLayout.SOUTH);
		field.setToolTipText("<html>Stas<br> powel</html>");
		field.setDocument(new TextLimitDocument(5));
		myLayer.add(new JLabel("Stas"), BorderLayout.WEST);
		myLayer.add(getTextField(), BorderLayout.EAST);
		JTextArea area = new JTextArea() {
			public JToolTip createToolTip() {
				return new MultiRowToolTip();
			}
		};
		area.setEditable(false);
		area
				.setText("Stas powel gulyat! Blay bla bla!\nYarik smotrit Flex, Tema robit' engine!");
		myLayer.add(area, BorderLayout.CENTER);
		area
				.setToolTipText("Stas powel gulyat! Blay bla bla!Yarik smotrit Flex, Tema robit' engine!");
		address.add(myLayer);
		this.addTab("1 tab", address);
		AddressPanel address2 = new AddressPanel();
		address2.setLayout(new GridLayout(0, 2));
		JPanel myLayer2 = new JPanel(new BorderLayout());
		table = new FlexibleTable();
		Object[] header = new Object[] { "Stas powel gulyat", "Yarik1",
				"Vitya1" };
		TableHelper tUtil = new TableHelper(table, header, true);
		JScrollPane scroll = tUtil.getScrollWithRowHeader();
		myLayer2.add(scroll, BorderLayout.CENTER);
		Object[] row = new Object[] { "Stas fsdf sdf ", "Yarik1", "Vitya1" };
		tUtil.addRow(row);
		row = new Object[] { "Stas21", "Yarik2", "Vitya2" };
		tUtil.addRow(row);
		row = new Object[] { "Stas32", "Yarik3", "Vitya3" };
		tUtil.addRow(row);
		row = new Object[] { "Stas31", "Yarik3", "Vitya3" };
		tUtil.addRow(row);
		row = new Object[] { "Stas33", "Yarik3 sdgsdf g sfdg ", "Vitya3" };
		tUtil.addRow(row);
		row = new Object[] { "Stas34", "Yarik3", "Vitya3" };
		tUtil.addRow(row);
		row = new Object[] { "Stas35", "Yarik3", "Vitya3" };
		tUtil.addRow(row);
		row = new Object[] { "Stas35", "Yarik3",
				"Vitya3sdf g sdfg dfgs dfgsd g" };
		tUtil.addRow(row);
		row = new Object[] { "Stas36", "Yarik3", "Vitya3" };
		tUtil.addRow(row);
		row = new Object[] { "Stas38", "Yarik3", "Vitya3" };
		tUtil.addRow(row);
		row = new Object[] { "Stas37", "Yarik3", "Vitya3" };
		tUtil.addRow(row);
		JTextArea area2 = new JTextArea();
		area2.setSize(new Dimension(200, 200));
		// myLayer2.add(area2, BorderLayout.NORTH);
		NumberFieldFormatter formatter = new NumberFieldFormatter();
		SelectableFormattedField text = new SelectableFormattedField(formatter);
		// text.addCaretListener(this);
		myLayer2.add(text, BorderLayout.NORTH);

		copyButton.addActionListener(this);
		myLayer2.add(copyButton, BorderLayout.EAST);
		// myLayer2.add(new JLabel("<html>Stas<br> powel</html>"),
		// BorderLayout.WEST);
		address2.add(myLayer2);
		JPanel myLayer3 = new JPanel(new GridLayout(2, 0));
		ButtonGroup bg = new ButtonGroup();
		address2.add(myLayer3);
		this.addTab("2 tab", address2);
		// cField.setUpdateText("1234%");
		/*
		 * closeA = new CloseTabAction(); JPopupMenu menu = new JPopupMenu();
		 * menu.add(new JMenuItem(closeA)); addMouseListener(new
		 * TabPopupListener(menu));
		 */
	}

	CompleterTextField cField;

	private JPanel getTextField() {
		InnFormatter formatter = new InnFormatter();
		ArrayList<String> inns = new ArrayList<String>();
		inns.add("1234%");
		inns.add("1444%");
		inns.add("1264%");
		inns.add("1574%");
		inns.add("1284%");
		inns.add("1239%");
		inns.add("1230%");
		inns.add("2234%");
		inns.add("2254%");
		inns.add("2254%");
		inns.add("2344%");
		cField = new CompleterTextField(inns.toArray());
		cField.setPreferredSize(new Dimension(120, 22));
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		panel.add(cField);
		panel.add(Box.createGlue());
		panel.add(Box.createGlue());
		panel.add(Box.createVerticalStrut(20));
		panel.add(Box.createVerticalStrut(20));
		return panel;
	}

	public void stateChanged(ChangeEvent e) {
		System.out.println("Stas");
	}

	public void myFireStateChanged() {
		super.fireStateChanged();
	}

	/*
	 * class TabPopupListener extends PopupMenuListener{ public
	 * TabPopupListener(JPopupMenu popup){ super(popup); } protected void
	 * maybeShowPopup(MouseEvent e){ int index =
	 * TabbedPane.this.getUI().tabForCoordinate(TabbedPane.this, e.getX(),
	 * e.getY()); if(index < 0) return; super.maybeShowPopup(e); } } public
	 * class CloseTabAction extends AbstractAction{ public CloseTabAction(){
	 * super("Close"); putValue(SHORT_DESCRIPTION, "Close2"); } public void
	 * actionPerformed(ActionEvent e){ int i = getSelectedIndex(); int count =
	 * getComponentCount(); if(count!=i+1){ fireStateChanged(); }
	 * remove(getSelectedComponent()); } }
	 */
	public void caretUpdate(CaretEvent e) {
		// TODO Auto-generated method stub
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == notModalButton) {
			MainFrame mf = new MainFrame(owner, false);
		} else if (e.getSource() == modalButton) {
			MainFrame mf = new MainFrame(owner, true);
		} else if (e.getSource() == copyButton) {
			CardReportBuilder report = new CardReportBuilder(table);
			report.buildDocumentRTF();
		}
	}
}
